#pragma once

#include "IAudioStream.h"
#include "portaudio.h"
#include "exception.h"

#define SAMPLE_RATE  (44100)
#define FRAMES_PER_BUFFER (512)
#define NUM_SECONDS     (5)
#define NUM_CHANNELS    (2)
#define DITHER_FLAG     (0) 
#define WRITE_TO_FILE   (0)
/* #define DITHER_FLAG     (paDitherOff) */
/* #define SAMPLE_RATE  (17932) // Test failure to open with this value. */

/* Select sample format. */
#if 1
#define PA_SAMPLE_TYPE  paFloat32
typedef float SAMPLE;
#define SAMPLE_SILENCE  (0.0f)
#define PRINTF_S_FORMAT "%.8f"
#elif 1
#define PA_SAMPLE_TYPE  paInt16
typedef short SAMPLE;
#define SAMPLE_SILENCE  (0)
#define PRINTF_S_FORMAT "%d"
#elif 0
#define PA_SAMPLE_TYPE  paInt8
typedef char SAMPLE;
#define SAMPLE_SILENCE  (0)
#define PRINTF_S_FORMAT "%d"
#else
#define PA_SAMPLE_TYPE  paUInt8
typedef unsigned char SAMPLE;
#define SAMPLE_SILENCE  (128)
#define PRINTF_S_FORMAT "%d"
#endif

typedef struct
{
    int          frameIndex;  /* Index into sample array. */
    int          maxFrameIndex;
	SAMPLE      *recordedSamples;
} paTestData;

#define TABLE_SIZE   (200)
//typedef struct
//{
//	float sine[TABLE_SIZE];
//	int left_phase;
//	int right_phase;
//	char message[20];
//}
//paTestData;

class IAudio;

class AudioStream : public IAudioStream
{
public:
	AudioStream(StreamType type);
	virtual ~AudioStream(void);

	virtual int Open(IAudio *audio, void *config, void *callback, void *data);
	virtual int Start();
	virtual int	IsActive();
	virtual int	Stop();
	virtual int Close();

private:
	PaError				state;
	StreamType			type;
	PaStreamParameters	*config;
	PaStream			*stream;
	PaStreamCallback	*callback;
};

